import { request } from './request';

export function loginUser(params: Record<string, any>) {
  return request('/api/user/getUserToken', { method: 'GET', params });
}

export function getBindAll(params?: Record<string, any>) {
  return request('/api/user/getShopList', { method: 'GET', params });
}

// 店铺数据类型定义
export type ShopData = {
  id: number;
  shopId: string;
  shopName: string;
  platform: string;
  createTime: string;
  updateTime: string;
};

export type GetBindAllResponse = {
  code: number;
  msg: string;
  data: ShopData[];
  '01K5TQMVKM6FDKVET1YSDDR453': string;
};

export function findShopList(params?: Record<string, any>) {
  return request('/api/user/findShopList', { method: 'GET', params });
}

export function delBind(params: Record<string, any>) {
  return request('/api/user/delBind', { method: 'GET', params });
}
// url：/api/user/saveUserForDB
// post
// 参数：loginId、userId、memberId
export function saveUserForDB(data: Record<string, any>) {
  return request('/api/user/saveUserForDB', { method: 'POST', data });
}

// 提交商品复制任务
export function submitCopyTask(data: { shopId: string; urls: string[] }) {
  return request('/api/copy/submitCopyTask', { method: 'POST', data });
}

// 提交商品复制任务响应类型
export type SubmitCopyTaskResponse = {
  code: number;
  msg: string;
  data: null;
};

// 复制记录列表
export function getCopyList(params: {
  platform?: string;
  tagName: string;
  itemId?: string;
  title?: string;
  pageNum: number;
  pageSize?: number;
}) {
  return request('/api/copy/getList', { method: 'GET', params });
}

// 复制记录数据类型
export type CopyLogData = {
  id: null;
  shopName: string;
  platform: string;
  aliItemId: string;
  aliItemTitle: string;
  aliCategory: string;
  copyState: number; // -1-复制失败 0-待复制，1-复制中，2-复制成功
  ecItemId: string;
  createTime: string;
};

// 复制记录列表响应类型
export type GetCopyListResponse = {
  code: number;
  msg: string;
  data: CopyLogData[];
};

// 订单列表查询
export function getTradeList(params: {
  tagName: string;
  shopId: string;
  startTime: string;
  endTime: string;
  oid?: string;
  title?: string;
  pageNum: string;
  pageSize: string;
}) {
  return request('/api/trade/getTradeList', { method: 'GET', params });
}

// 关联货源信息类型
export type RelationObj = {
  dgMinNum: number;
  dgSkuId: string;
  dgTitle: string;
  dgImgUrl: string;
  dgSellerNick: string;
  dgItemId: string;
  dgSkuDesc: string;
  dgSpecid: string;
};

// 代购订单数据类型
export type PurchaseData = {
  dgOid: string; // 代购订单号
  shopId: number; // 店铺id
  oid: number; // 订单号
  payTime: string; // 支付时间
  productId: number; // 商品id
  skuId: number; // skuid
  skuDesc: string; // sku描述
  status: string; // 代购订单状态: 已拍单、已取消、已发货
  dgUser: string; // 拍单号
  dgPlatform: string; // 代购平台
  dgPlatformBuyerNick: string; // 代购平台买家昵称
  dgPlatformUserId: string; // 代购平台买家ID
  dgPlatformSellerNick: string; // 代购平台卖家昵称
  dgItemId: string; // 代购商品ID
  dgItemTitle: string; // 代购商品名称
  dgItemPic: string; // 代购商品图片
  dgSkuId: string; // 代购skuid
  dgSkuDesc: string; // 代购商品sku
  dgPayment: number; // 代购商品价格
  dgNum: number; // 代购件数
  dgExpressNo: string; // 代购商品物流单号
  dgExpressName: string; // 代购商品物流公司
  checkStatus: string; // 检测状态（包含售后状态）
  checkTime: string; // 检测时间
  createTime: string; // 创建时间
  updateTime: string; // 更新时间
  relMobile: string; // 买家真实手机号
  sendSms: boolean; // 是否发送了短信
  dgAfterSaleId: string; // 售后ID
  afterSaleComplete: boolean; // 售后完成 0-否 1-是
  dgAfterSaleStatus: string; // 已废弃
  dgAfterSaleDescription: string; // 已废弃
  dgAfterSaleTime: string; // 已废弃
  isFenxiao: boolean; // 是否为1688分销
  ext_str1: string; // 扩展字段1
  ext_str2: string; // 自动退款处理状态
  ext_str3: string; // 扩展字段3
  ext_int1: number; // 是否使用虚拟号/指定号码
  ext_int2: number; // 分销订单是否支付
  ext_int3: number; // 标识是否使用双物流发货
};

// 订单数据类型
export type TradeData = {
  itemPicUrl: string;
  buyerNick: string;
  payTime: string;
  promiseSendTime: string;
  townCode: number;
  num: number;
  remindShipmentSign: number;
  relationObj: RelationObj;
  consigneeStr: string;
  address_str: string;
  expressFee: number;
  mobile_str: string;
  buyerImage: string;
  payType: number;
  price: number;
  spare_str3: string;
  shopId: number;
  isolationType: number;
  skuId: number;
  consignee: string;
  town: string;
  riskCode: string;
  itemId: number;
  itemLinkUrl: string;
  skuNick: string;
  estimatedIncome: number;
  status: number; // 订单状态：[0, "未知状态"], [10, "待付款"], [30, "已付款"], [40, "已发货"], [50, "已签收"], [70, "订单成功"], [80, "订单失败"]
  exceptionType: number;
  commissionRate: number;
  districtCode: number;
  flag: string;
  itemTitle: string;
  cityCode: number;
  shopName: string;
  refundStatus: number;
  remark: string;
  oid: number;
  shopNewBuyer: boolean;
  skuDesc: string;
  buyerOpenId: string;
  address: string;
  provinceCode: number;
  platformNewBuyer: boolean;
  mobile: string;
  isPurchase: boolean;
  preSale: number;
  sellerNote: string;
  totalFee: number;
  auditStatus: number;
  purchaseList: PurchaseData[]; // 代购订单列表
};

// 订单列表响应类型
export type GetTradeListResponse = {
  msg: string;
  code: number;
  data: TradeData[];
  totalCount: number;
};

// 分销下单
export function createFenxiaoOrder(data: {
  shopId: string;
  oid: string;
  coid: string;
  itemId: string;
  dgItemId: string;
  dgTitle: string;
  dgImgUrl: string;
  dgSkuId: string;
  dgSkuDesc: string;
  dgSpecid: string;
  dgNum: number;
  isCiphertext: boolean;
  dgPlatformUserId: string;
  dgPlatformUserName: string;
  dgSellerNick: string;
  isPay: boolean;
  flow: string;
}) {
  return request('/api/trade/createFenxiaoOrder', { method: 'POST', data });
}

// 分销下单响应类型
export type CreateFenxiaoOrderResponse = {
  code: number;
  msg: string;
  data: null;
};

// 查询商品关联信息
export function getSkuRelationTo(params: {
  shopId: string;
  itemId?: string;
}) {
  return request('/api/product/getSkuRelationTo', { method: 'GET', params });
}

// 关联商品信息类型
export type RelatedDTO = {
  serialNo: number;
  linkUrl: string;
  dgGoodsId: string;
  commissionUrl: string;
  minNumber: number;
  dgPlatform: string;
};

// SKU信息类型
export type SkuDTO = {
  shopId: number;
  shopName: string;
  goodsId: number;
  skuImage: string;
  skuId: number;
  skuDesc: string;
  skuDescMain: string;
  skuDescVice: string;
  dgGoodsId: string;
  dgTitle: string;
  dgImgUrl: string;
  dgSkuId: string;
  dgSkuIdList: string[];
  dgSkuDesc: string;
  dgSkuDescVerify: string;
  dgLink: string;
  dgCommissionUrl: string;
  dgPlatform: string;
  dgSellerId: string;
  dgShopName: string;
  dgSpecId: string;
  dgMinnum: number;
  isCiphertext: boolean;
};

// 商品关联信息响应类型
export type GetSkuRelationToResponse = {
  msg: string;
  code: number;
  data: {
    imgUrl: string;
    relatedDTOList: RelatedDTO[];
    goodsId: number;
    isSelected: boolean;
    shopId: number;
    goodsName: string;
    skuDTOList: SkuDTO[];
    skuCount: number;
    bindSkuCount: number;
  };
};
// 1688商品SKU查询
export function getSkus(params: { offerId: string }) {
  return request('/api/product/getSkus', { method: 'GET', params });
}

// 1688商品SKU数据类型
export type AliSkuData = {
  aliItemId: string;
  aliSpecid: string;
  aliImgUrl: string;
  aliSellerNick: string;
  aliSkuId: number;
  aliTitle: string;
  aliSkuDesc: string;
};

export type GetSkusResponse = {
  msg: string;
  code: number;
  data: AliSkuData[];
};

// 保存商品关联
export function saveRelationSku(data: {
  shopId: string;
  shopName: string;
  goodsId: number;
  skuImage: string;
  skuId: number;
  skuDesc: string;
  dgGoodsId: string;
  dgTitle: string;
  dgImgUrl: string;
  dgSkuId: string;
  dgSkuDesc: string;
  dgLink: string;
  dgPlatform: string;
  dgSellerId: string;
  dgShopName: string;
  dgSpecId: string;
  dgMinnum: number;
  isCiphertext: boolean;
  
}) {
  return request('/api/product/saveRelationSku', { method: 'POST', data });
}

export type SaveRelationSkuResponse = {
  code: number;
  msg: string;
  data: null;
};

export const LoginFunc = {
  loginUser,
  getBindAll,
  findShopList,
  delBind,
  saveUserForDB,
  submitCopyTask,
  getCopyList,
  getTradeList,
  createFenxiaoOrder,
  getSkuRelationTo,
  getSkus,
  saveRelationSku,
};


